<template>
  <div>
    <pro-table
      ref="tableRef"
      :data="tableData"
      :total="total"
      :columns="columns"
      :tool-button="false"
      @search="commEvent.onSearch"
      :page-change="commEvent.onPageChange"
      :tree-props="{ children: 'children', hasChildren: 'hasChildren' }"
    >
      <!-- 顶部配置项 -->
      <template #tableHeader>
        <el-row justify="end">
          <el-button type="primary" icon="Plus" @click="commEvent.onSave()">新增</el-button>
        </el-row>
      </template>
      <!-- 操作 -->
      <template #operation="{ row }">
        <!-- <el-button type="primary" link @click="commEvent.onSave(row)">编辑</el-button> -->
        <el-button type="danger" link @click="commEvent.onDelete(row)">删除</el-button>
      </template>
    </pro-table>
    <!-- 新增/编辑弹窗 -->
    <save-device
      v-model="saveVisible"
      :rows="currRow"
      :factorys="factoryEnum"
      :lines="lineEnum"
      :devicet-types="deviceTypeEnum"
      :energy-grades="energyGradeEnum"
      @refresh="commEvent.onSearch"
    />
  </div>
</template>

<script setup lang="ts">
import { onMounted, ref } from "vue"
import ProTable from "@/components/ProTable/index.vue"
import SaveDevice from "./components/SaveDevice.vue"
import { ColumnProps, EnumProps, ProTableInstance } from "@/components/ProTable/interface"
import { ElMessage, ElMessageBox } from "element-plus"
import { stopPage, removeStop } from "@/api/modules/device"
import { dataDictionaryByType, organByLevel } from "@/api/modules/system"
import { SystemSpace } from "@/api/interface/device"
import { formatDate } from "@/utils"
// 参数
const tableRef = ref<ProTableInstance>() // 表格实例
const tableData = ref<SystemSpace.stopPage[]>([]) // 表格数据
const total = ref<number>(0) // 数据总数
const saveVisible = ref<boolean>(false) // 新增编辑弹窗
const currRow = ref<SystemSpace.stopPage>() // 当前选中数据
const factoryEnum = ref<EnumProps[]>([]) // 厂区
const lineEnum = ref<EnumProps[]>([]) // 产线
const deviceTypeEnum = ref<EnumProps[]>([]) // 设备类型
const energyGradeEnum = ref<EnumProps[]>([]) // 能耗等级
// 页码参数
const pageParams = ref({
  pageIndex: 1,
  pageSize: 10
})
// 表格列配置
const columns: ColumnProps<SystemSpace.stopPage>[] = [
  { prop: "factoryId", label: "所属厂区", width: 160, enum: factoryEnum, search: { el: "select" } },
  { prop: "lineId", label: "所属产线", width: 160, enum: lineEnum, search: { el: "select" } },
  { prop: "delFlag", label: "删除标志", minWidth: 220 },
  {
    prop: "stopDate",
    label: "停止日期",
    width: 160,
    render: (scope: any) => formatDate(scope.row.stopDate)
  },
  { prop: "stopTimes", label: "停止时间", width: 160 },
  {
    prop: "createTime",
    label: "创建时间",
    width: 160
  },
  { prop: "createBy", label: "创建人", width: 160 },
  {
    prop: "updateTime",
    label: "更新时间",
    width: 160
  },
  { prop: "updateBy", label: "更新人", width: 160 },
  { prop: "remark", label: "备注", width: 300 },
  { prop: "operation", label: "操作", width: 120, fixed: "right" }
]
// 事件集合
const commEvent = {
  // 获取所属厂区
  getFactory: async () => {
    const { data } = await organByLevel(1)
    factoryEnum.value = data.map(item => ({
      label: item.organName,
      value: item.id
    }))
  },
  // 获取所属产线
  getLine: async () => {
    const { data } = await organByLevel(2)
    lineEnum.value = data.map(item => ({
      label: item.organName,
      value: item.id
    }))
  },
  // 获取能耗等级
  getEnergyGrade: async () => {
    const { data } = await dataDictionaryByType("energy_grade")
    energyGradeEnum.value = data.map(item => ({
      label: item.dictLabel,
      value: Number(item.dictValue)
    }))
  },
  // 获取表格数据
  getTableData: async (params: SystemSpace.stopPageQuery) => {
    const { data } = await stopPage(params)
    tableData.value = data.result
    total.value = data.totalCount
  },
  // 查询
  onSearch: () => {
    tableRef.value?.search()
    pageParams.value.pageIndex = 1
    const _search = tableRef.value?.searchParam
    commEvent.getTableData({ ...pageParams.value, ..._search })
  },
  // 分页
  onPageChange: (page: number, size: number) => {
    pageParams.value.pageIndex = page
    pageParams.value.pageSize = size
    const _search = tableRef.value?.searchParam
    commEvent.getTableData({ ...pageParams.value, ..._search })
  },
  //  新增/编辑
  onSave: (row?: SystemSpace.stopPage) => {
    currRow.value = row
    saveVisible.value = true
  },
  // 删除
  onDelete: async (row: SystemSpace.stopPage) => {
    ElMessageBox.confirm("确定删除此设备?", "删除", {
      confirmButtonText: "确定",
      cancelButtonText: "取消",
      type: "warning"
    })
      .then(async () => {
        const { code } = await removeStop({ id: row.id })
        if (code !== "200") return
        ElMessage.success("删除成功")
        commEvent.onSearch()
      })
      .catch(() => {})
  }
}
onMounted(() => {
  commEvent.getTableData(pageParams.value)
  commEvent.getEnergyGrade()
  commEvent.getFactory()
  commEvent.getLine()
})
</script>

<style lang="scss" scoped></style>
